package com.paw.jest.controller;


import java.io.IOException;

import com.google.gson.JsonObject;

import cn.hutool.core.util.StrUtil;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Search;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;



/**
 * @author: lvzihai
 * @date: 2021/5/24
 **/
@Slf4j
@RestController
@Api(tags = "ES管理器")
public class EsController {


  @Autowired
  private JestClient jestClient;


  @ApiOperation("jest搜索")
  @GetMapping("/search")
  public Object search(){
    String appName = "product-api";
    String level="ERROR";

    String indexName = "logs-2021.05.25";

    String query1 = "{\n"
        + "  \"query\": {\n"
        + "    \"match_all\": {}\n"
        + "  }\n"
        + "}";

    String query = "{\n"
        + "  \"from\":0,\"size\":10000,\n"
        + "  \"query\": {\n"
        + "    \"bool\": {\n"
        + "      \"filter\": [\n"
        + "          {\n"
        + "            \"term\": {\n"
        + "              \"appName.keyword\": {\n"
        + "                \"value\": \"activity\"\n"
        + "              }\n"
        + "            }\n"
        + "        },\n"
        + "        {\n"
        + "          \"term\": {\n"
        + "            \"level.keyword\": {\n"
        + "              \"value\": \"ERROR\"\n"
        + "            }\n"
        + "          }\n"
        + "        }\n"
        + "      ]\n"
        + "    }\n"
        + "  }\n"
        + "}";
        //
//    JestResult jr = null;
    String result = "success";
    try {
      Search search1 = new Search.Builder(query).addIndex(indexName).addType("_doc").build();
      JestResult jr = jestClient.execute(search1);
      System.out.println("--++"+jr.getJsonString());
      result = jr.getJsonString();

      SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
      searchSourceBuilder.query(QueryBuilders.boolQuery()
          .filter(QueryBuilders.termQuery("appName.keyword",appName))
          .filter(QueryBuilders.termQuery("level.keyword",level)));
      searchSourceBuilder.from(0).size(10000);

      System.out.println("query:\n"+searchSourceBuilder.toString());
      Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex(indexName).addType("_doc").build();
      JestResult jestResult = jestClient.execute(search);
      JsonObject resultJSON = jestResult.getJsonObject();
      JsonObject totalJson = resultJSON.getAsJsonObject("hits").getAsJsonObject("total");
      long totalNum = totalJson.get("value").getAsLong();
      // eq  gte
      String relation = totalJson.get("relation").getAsString();

      System.out.println("==============="+jestResult.getJsonString());

    } catch (IOException e) {
      e.printStackTrace();
    }

    return result;

  }


}
